import { updateDocument, deserializeTags } from "../../service/agent-document"; import { requireUser } from "#server/utils/context"; import { validate } from "#server/utils/validation"; import { z } from "zod"; const updateSchema = z.object({ title: z.string().min(1).max(200).optional(), content: z.string().max(50000).optional(), tags: z.array(z.string()).optional(), }); export default defineWrappedResponseHandler(async (event) => { const user = await requireUser(event); const idParam = getRouterParam(event, "id"); if (!idParam) return R.throwError(400, "缺少文档 ID", null); const id = parseInt(idParam); if (isNaN(id)) return R.throwError(400, "文档 ID 格式不正确", null); const body = await validate(updateSchema, await readBody(event)); const updated = await updateDocument(id, user.id, { title: body.title, content: body.content, tags: body.tags, }); if (!updated) return R.throwError(404, "文档不存在或无权访问", null); return R.success({ ...updated, tags: deserializeTags(updated.tags), }); });